package com.bacon.gmall.canal.config;

import com.alibaba.otter.canal.protocol.CanalEntry;
import com.bacon.gmall.common.constant.RedisConst;
import com.bacon.gmall.model.cart.CartInfo;
import com.xpand.starter.canal.annotation.CanalEventListener;
import com.xpand.starter.canal.annotation.ListenPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import java.math.BigDecimal;

/**
 * @author chen.qian
 * @date 2018/3/19
 */
@CanalEventListener
public class MyEventListener {

    @Autowired
    private RedisTemplate redisTemplate;


    /*@InsertListenPoint
    public void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
    }

    @UpdateListenPoint
    public void onEvent1(CanalEntry.RowData rowData) {
        System.err.println("UpdateListenPoint");
        rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
    }
*/
   /* @DeleteListenPoint
    public void onEvent3(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        List<CanalEntry.Column> afterColumnsList = rowData.getBeforeColumnsList();
        CartInfo cartInfo = new CartInfo();
        for (CanalEntry.Column element : afterColumnsList) {


        }
        redisTemplate.opsForHash().delete(RedisConst.USER_KEY_PREFIX + cartInfo.getUserId() + RedisConst.USER_CART_KEY_SUFFIX,
                cartInfo.getSkuId().toString());
    }*/

    @ListenPoint(destination = "example",
            schema = "gmall_order",
            table = {"cart_info"},
            eventType = {
                    CanalEntry.EventType.UPDATE,
                    CanalEntry.EventType.INSERT,
                    CanalEntry.EventType.DELETE})
    public void onEvent4(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
        CartInfo cartInfo = new CartInfo();
        rowData.getAfterColumnsList().forEach(element -> {
            if ("id".equals(element.getName())) {
                cartInfo.setId(Long.parseLong(element.getValue()));
            } else if ("user_id".equals(element.getName())) {
                cartInfo.setUserId(element.getValue());
            } else if ("sku_id".equals(element.getName())) {
                cartInfo.setSkuId(Long.parseLong(element.getValue()));
            } else if ("cart_price".equals(element.getName())) {
                cartInfo.setCartPrice(new BigDecimal(element.getValue()));
            } else if ("sku_num".equals(element.getName())) {
                cartInfo.setSkuNum(Integer.parseInt(element.getValue()));
            } else if ("img_url".equals(element.getName())) {
                cartInfo.setImgUrl(element.getValue());
            } else if ("sku_name".equals(element.getName())) {
                cartInfo.setSkuName(element.getValue());
            } else if ("is_checked".equals(element.getName())) {
                cartInfo.setIsChecked(Integer.parseInt(element.getValue()));
            }
        });

        redisTemplate.opsForHash().put(RedisConst.USER_KEY_PREFIX + cartInfo.getUserId() + RedisConst.USER_CART_KEY_SUFFIX,
                cartInfo.getSkuId().toString(),
                cartInfo);
    }
}
